package com.dfch.auto.appium.app;

import com.dfch.auto.appium.utils.TextUtils;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.ios.IOSElement;
import io.appium.java_client.remote.AutomationName;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Platform;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.URL;

/**
 * @author ldcmaxim on 05/04/2018.
 */
public abstract class AutoiOS extends AutoApp {
    private static final String APP_PLATFORM = "iOS";
    private IOSDriver<IOSElement> mIOSDriver;

    public AutoiOS() {
    }

    @Override
    public void release() {
        if (mIOSDriver != null) {
            mIOSDriver.quit();
        }
        mIOSDriver = null;
    }


    protected boolean connect(String url, Capabilities cap) {
        if (TextUtils.isEmpty(url) || cap == null) {
            return false;
        }
        try {
            mIOSDriver = new IOSDriver<IOSElement>(new URL(url), cap);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    protected DesiredCapabilities getCapabilities() {
        DesiredCapabilities cap = new DesiredCapabilities();
        // Platform
        cap.setCapability(MobileCapabilityType.PLATFORM_NAME, Platform.IOS);
        // Reset app and clear cache
        cap.setCapability(MobileCapabilityType.NO_RESET, true);
        // Set automation type
        cap.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.IOS_XCUI_TEST);
        return cap;
    }

    protected IOSDriver<IOSElement> getDriver() {
        return mIOSDriver;
    }


    protected boolean isConnected() {
        return mIOSDriver != null;
    }

    @Override
    public String getAppPlatform() {
        return APP_PLATFORM;
    }
}
